package com.oig.sys.auth.service.impl;

import com.oig.common.constants.CommonEnum;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.auth.dao.ITbPermissionDao;
import com.oig.sys.auth.dao.ITbRolePermissionDao;
import com.oig.sys.auth.dto.PermissionDto;
import com.oig.sys.auth.entity.TbRolePermission;
import com.oig.sys.auth.service.ITbRolePermissionService;
import com.oig.sys.auth.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuxl
 */
@Service
public class TbRolePermissionServiceImpl implements ITbRolePermissionService {

	private final ITbRolePermissionDao tbRolePermissionDao;
	private final ITbPermissionDao tbPermissionDao;

	@Autowired
	public TbRolePermissionServiceImpl(ITbRolePermissionDao tbRolePermissionDao, ITbPermissionDao tbPermissionDao){
		 this.tbRolePermissionDao = tbRolePermissionDao;
		 this.tbPermissionDao = tbPermissionDao;
	}


	@Override
	public List<PermissionVo> findPermissionByRole(Integer roleId) {
		List<Integer> permissionIdList = this.tbRolePermissionDao.findPermissionIdByRoleId(roleId);
		return this.tbPermissionDao.findByIdList(permissionIdList);
	}

	@Override
	public List<PermissionVo> findOkPermissionByRole(List<Integer> roleIdList) {
		List<PermissionVo> permissionVoList = new ArrayList<>() ;
		List<Integer> permissionIdList = this.tbRolePermissionDao.findPermissionIdByRoleIdList(roleIdList, CommonEnum.CommonStatusEnum.OK.getCode());
		List<PermissionVo> rolePist = this.tbPermissionDao.findByIdList(permissionIdList);	//角色权限
		List<PermissionVo> noAuthPList = this.tbPermissionDao.findByNoAuthList();			//不需要验证的直接算有权限
		if (rolePist!=null) permissionVoList.addAll(rolePist);
		if (noAuthPList!=null) permissionVoList.addAll(noAuthPList);
		return permissionVoList;
	}

	@Transactional
	@Override
	public List<PermissionVo> addRolePermission(Integer roleId, List<PermissionDto> permissionDtoList) {
		List<Integer> permissionIdList = new ArrayList<>();
		for (PermissionDto permissionDto : permissionDtoList){
			if (permissionDto.getId()==null){
				throw new BadRequestException("权限id为空");
			}
			TbRolePermission rolePermission = new TbRolePermission();
			rolePermission.setRoleId(roleId);
			rolePermission.setPermissionId(permissionDto.getId());
			this.tbRolePermissionDao.save(rolePermission) ;
			permissionIdList.add(rolePermission.getPermissionId()) ;
		}
		return tbPermissionDao.findByIdList(permissionIdList);
	}

	@Transactional
	@Override
	public void addRolePermissionId(Integer roleId, List<Integer> permissionIdList) {
		for (Integer permissionId : permissionIdList) {
			TbRolePermission rolePermission = new TbRolePermission();
			rolePermission.setRoleId(roleId);
			rolePermission.setPermissionId(permissionId);
			this.tbRolePermissionDao.save(rolePermission);
		}
	}

	@Transactional
	@Override
	public void delRolePermissionId(Integer roleId, List<Integer> permissionIdList) {
		List<Integer> delIdList = this.tbRolePermissionDao.findAllDelId(roleId, permissionIdList);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbRolePermissionDao.deleteByIdList(delIdList) ;
		}

	}


	private TbRolePermission addNoPermission(Integer roleId, Integer permissionId){
		TbRolePermission rolePermission = this.tbRolePermissionDao.getByRoleIdAndPermissionId(roleId, permissionId) ;
		if (rolePermission==null){
			rolePermission = new TbRolePermission();
			rolePermission.setRoleId(roleId);
			rolePermission.setPermissionId(permissionId);
			this.tbRolePermissionDao.save(rolePermission) ;
		}
		return rolePermission ;
	}


	@Transactional
	@Override
	public List<PermissionVo> updateRolePermission(Integer roleId, List<PermissionDto> permissionDtoList) {
		List<Integer> permissionIdList = new ArrayList<>();
		List<Integer> rolePermissionIdList = new ArrayList<>() ;
		for (PermissionDto permissionDto : permissionDtoList) {
			if (permissionDto.getId()==null){
				throw new BadRequestException("权限id为空");
			}
			TbRolePermission rolePermission = this.addNoPermission(roleId, permissionDto.getId());
			rolePermissionIdList.add(rolePermission.getId()) ;
			permissionIdList.add(rolePermission.getPermissionId()) ;
		}
		List<Integer> delIdList = this.tbRolePermissionDao.findAllDelId(roleId, rolePermissionIdList);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbRolePermissionDao.deleteByIdList(delIdList) ;
		}
		return tbPermissionDao.findByIdList(permissionIdList);
	}

	@Transactional
	@Override
	public void delRolePermissionByRoleId(Integer roleId) {
		List<Integer> delIdList = this.tbRolePermissionDao.findAllIdByRoleId(roleId);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbRolePermissionDao.deleteByIdList(delIdList) ;
		}
	}


	@Transactional
	@Override
	public void addAllRoleNewPermission(List<Integer> roleIdList, List<PermissionVo> permissionVoList) {
		for (Integer roleId : roleIdList){
			for (PermissionVo permissionVo : permissionVoList){
				addNoPermission(roleId, permissionVo.getId());
			}
		}
	}

}
